<?php
   namespace app\admin\controller;
   use \think\Db;
   class Fl extends Memcache{ 

   	 public $arr=array();
      function brand($pid=0,$level=0){
      	$call=input('get.callback');
          if(!$fl=$this->mem->get('fl') or !$list=$this->mem->get('list')){
          	    $_res=Db::name('fl')->select();
          	    foreach($_res as $v){
                     $res[$v['id']]=$v;
          	    }
              $arr=$this->sort($res);
          	    /*$one=[];
                $tow=[];
                $three=[];
                $list=[];
          	    foreach($res as $v){
          	    	$list[$v['id']]=$v;
          	    }
          	    foreach($list as $v){ 
          	        if($v['fl_cid']=='0'){
          	            $one[$v['id']]=$v;
                 	}elseif(array_key_exists($v['fl_cid'],$list) and $list[$v['fl_cid']]['fl_cid']==0){
                        $tow[$v['fl_cid']][$v['id']]=$v;
          	        }else{
                         $three[$v['fl_cid']][]=$v;
          	        }
               }
                foreach($tow as $b=>$a){
                  foreach($a as $bb=>$aa){
                  	 	 if(array_key_exists($bb,$three)){
                  	 	 	  $tow[$b][$bb]['tow']=$three[$bb];
                  	 	 }
                  }	
                }
              $arr=[];
               foreach($one as $a){
                  if(array_key_exists($a['id'],$tow)){
                      $one[$a['id']]['one']=$tow[$a['id']];
                  }
               }*/
               $this->mem->set('fl',$arr);
          	   $fl=$arr;
          } 
         return $call.'('.json_encode(['fl'=>$fl]).')';
 
      }

    
     //添加分类
      function brand_save(){
            
           $call=input('get.');
           $res=Db::name('fl')->insert(['fl_name'=>$call['fl_name'],'fl_num'=>$call['fl_num'],'fl_cid'=>$call['fl_cid']]);
           if($res){
           	   $this->brand_mem();
              return $call['callback'].'('.json_encode(['status'=>'ok']).')';
           }else{
             return $call['callback'].'('.json_encode(['status'=>'fail']).')';
           }
      }


        /*
           $data 接收上面查询到的所有数据;
           pid  当前的父级id
	       level 	设置分类之间的显示提示;
        */
    //编辑分类(回显)
    function brand_echo(){
       $call=input('get.');
       $res=Db::name('fl')->find($call);
       if($res){
       	  $this->arr[$res['id']]=$res['fl_cid'];
          $sel=$this->brand_mem();
       	  return $call['callback'].'('.json_encode(['status'=>'ok','list'=>$res,'sel'=>$sel]).')';
       }else{
          return $call['callback'].'('.json_encode(['status'=>'fail']).')';
       }
    }

//修改分类
    function brand_edit($id=0){
       $call=input('get.');
       $res=Db::name('fl')->where('id',$id)->update(['fl_name'=>$call['fl_name'],'fl_num'=>$call['fl_num'],'fl_cid'=>$call['fl_cid']]);
       if($res){
          $sel=$this->brand_mem();
       	  return $call['callback'].'('.json_encode(['status'=>'ok','list'=>$res,'sel'=>$sel]).')';
       }else{
          return $call['callback'].'('.json_encode(['status'=>'fail']).')';
       }
    }

//删除分类
  function brand_del(){
  	 $call=input('get.');
  	 $res=Db::name('fl')->delete($call['id']);
  	 if($res){
          $sel=$this->brand_mem();
       	  return $call['callback'].'('.json_encode(['status'=>'ok']).')';
       }else{
          return $call['callback'].'('.json_encode(['status'=>'fail']).')';
     }
  }
    function brand_mem(){
        $_res=Db::name('fl')->select();
          	    foreach($_res as $v){
                     $res[$v['id']]=$v;
          	    }
                 if($this->arr!=null){
                 	$arr=$this->sort($res);
                 	return $arr;
                 }else{
                     $arr=$this->sort($res);
                     $this->mem->set('fl',$arr);
                 }
                
    }

    public function sort($data,$name='children',$category_pid=0,$level=0){
		$arr = array();
        foreach ($data as $k => $v) {
              if ($v['fl_cid'] == $category_pid) {
              	  $v['icon']='';
              	  $v['level']=$level;
              	  $v['opened']=false;
              	  $v['selected']=false;
              	  $v['disabled']=false;
              	  $v['loading']=false;
              	  $v['text']=$v['fl_name'];
              	  unset($v['fl_name']);
              	 if($this->arr!=null){
              	  foreach($this->arr as $vv){
              	  	 if(array_key_exists($vv,$data) and !in_array($data[$vv]['fl_cid'],$this->arr)){
              	  	 	 $this->arr[]=$data[$vv]['fl_cid'];
              	  	 }
              	  }
              	}

              	  
			      $v['children'] =  $this->sort($data,$name,$v['id'],$level+1);
                  
                   if(in_array($v['id'],$this->arr)){
              	  	 $v['opened']=true;
              	  }
              	  if(array_key_exists($v['id'],$this->arr) and $v['fl_cid']==$this->arr[$v['id']]){
              	  	 $v['selected']=true;
              	  }
              	  $arr[] = $v;
		        }
        }
       
        return $arr;
    }


    function get_tian(){
      $url="http://www.weather.com.cn/html/weather/101250501.shtml";
     $content = file_get_contents($url);
    $str="/<ul class=\"t clearfix\">(.*?)(.+?)<\/ul>/si";
    preg_match_all($str,$content,$arr);
    $ex=explode('<div class="slid"></div>',$arr[0][0]);
      $arr=[];
    foreach($ex as $k=>$v){
     $h="/<h1>(.+?)<\/h1>/";
     preg_match($h,$v,$h);//多少号 $h[1]
     $weather="/<p title=\"(.*)\" class=\"wea\">(.*)/i";
     preg_match($weather,$v,$weather);//什么天气 $weather[1]
     $grade="/<i>(.*)<\/i>/i";
     preg_match($grade,$v,$grade);//多少度 $grade[1]
     $wind="/<span title=\"(.*)\" class=\"N\"><\/span>/i";
      preg_match($wind,$v,$wind);//风向 $wind[1]
      $ji="/<i><(.*)<\/i>/i";
      preg_match($ji,$v,$ji);//例<3级 $ji[1]
      
      $arr[$k][]=['h'=>@$h[1],'weather'=>@$weather[1],'grade'=>@$grade[1],'wind'=>@$wind[1],'ji'=>@$ji[1]];
    }
       echo '<pre/>';
       var_dump($arr);
   }
   } 
?>
